
<template>
  <div class="password">
    <div class="topinput">
      <p>请输入身份证后六位</p>
      <div class="inputbox">
        <div class="warp">
          <span class="diamonds"
                :class="{'active':passwordAll[0]}"></span>
          <span class="diamonds"
                :class="{'active':passwordAll[1]}"></span>
          <span class="diamonds"
                :class="{'active':passwordAll[2]}"></span>
          <span class="diamonds"
                :class="{'active':passwordAll[3]}"></span>
          <span class="diamonds"
                :class="{'active':passwordAll[4]}"></span>
          <span class="diamonds"
                :class="{'active':passwordAll[5]}"></span>
        </div>
      </div>
      <p class="error"
         v-show="errflag">输入错误，请重新输入</p>
    </div>
    <div class="keyboard">
      <span v-for="(item,index) in numKey"
            :key="index"
            @click="handleNum(item,index)">{{item}}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Password',
  props: ['title'],
  data () {
    return {
      passwordAll: [],
      numKey: [1, 2, 3, 4, 5, 6, 7, 8, 9, 'X', 0, '删除'],
      idCardLastNum: null,
      errflag: false
    }
  },
  created () {
    // 获取身份证后六位
    let idCardlast = this.$store.state.idCard.substring(this.$store.state.idCard.length - 6, this.$store.state.idCard.length)
    this.idCardLastNum = idCardlast
    console.log(this.idCardLastNum)
  },
  methods: {
    handleNum (valNum, index) {
      this.errflag = false
      if (valNum !== '删除') { // 点击数字或x
        this.passwordAll.push(valNum.toString())
        let passLen = this.passwordAll.length
        if (passLen === 6) {
          let pwdstring = this.passwordAll.join('')
          if (pwdstring === this.idCardLastNum) {
            this.$router.push('/prepaymentinfo')
          } else {
            this.errflag = true
            setTimeout(() => {
              this.passwordAll = []
            }, 200)
            // this.passwordAll = []
          }
        } else if (passLen > 6) {
          this.passwordAll = this.passwordAll.slice(0, 6)
          return false
        }
      } else { // 点击删除
        this.passwordAll.pop()
        this.errflag = false
      }
    }
  }
}
</script>

<style style="stylus" scoped>
.password {
  width: 6rem;
  height: 7.5rem;
  background: #fff;
  border-radius: 0.2rem;
  box-shadow: 0px 11px 24px 0px rgba(127, 135, 143, 0.1);
  overflow: hidden;
  margin: 0 auto;
}
.topinput {
  text-align: center;
  padding-bottom: 0.6rem;
  background: #f7f8fa;
  position: relative;
}
.topinput p {
  font-size: 0.28rem;
  color: #27ae9e;
  padding: 0.4rem 0 0.2rem 0;
}
.topinput p.error {
  color: #f00;
  padding: 0;
  line-height: 1;
  font-size: 0.24rem;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0.17rem;
}
.inputbox {
  /* width: 4.94rem; */
  height: 0.82rem;
  margin: 0 auto;
  display: inline-block;
}
.inputbox .warp {
  display: block;
  clear: both;
  overflow: hidden;
  border: 0.01rem solid #e6e6e6;
}
.inputbox .warp span {
  width: 0.82rem;
  height: 0.82rem;
  line-height: 0.82rem;
  border: 0.01rem solid #e6e6e6;
  float: left;
}
.keyboard {
  clear: both;
  overflow: hidden;
  text-align: center;
}
.keyboard span {
  height: 1.29rem;
  width: 2rem;
  line-height: 1.29rem;
  font-size: 0.48rem;
  color: #333;
  float: left;
  border-right: 0.02rem solid #e6e6e6;
}
.diamonds.active::before {
  content: '';
  display: inline-block;
  width: 0.25rem;
  height: 0.25rem;
  border-radius: 50%;
  background: #1a1a1a;
}
.keyboard span:nth-child(3),
.keyboard span:nth-child(6),
.keyboard span:nth-child(9),
.keyboard span:nth-child(12) {
  border: 0;
}
.keyboard span:nth-child(12) {
  background: #f2f3f5;
}
</style>
